<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\api\controller;

use think\request;
use cmf\controller\HomeBaseController;

class IndexController extends HomeBaseController
{ 

	/**
	*
	*首页
	**/
    public function index()
    {
    	//首页数据展示
    	$activityModel = new \app\home\model\ActivityModel();
    	$courseModel = new \app\home\model\CourseModel();
    	$coulabelModel = new \app\home\model\CourseLabelModel();
        $couageModel = new \app\home\model\CourseAgeModel();
        $cousubjectModel = new \app\home\model\CourseSubjectModel();


        //获得轮播图、常驻活动、
        $banner_list = $activityModel->getActivityBannerList();//banner列表
        foreach ($banner_list as $key => $value) {
            $banner_list[$key]['logo'] = json_img_add_url($value['logo']);
        }

        $rukou_list = $activityModel->getActivityRukouList();//常驻入口列表
        foreach ($rukou_list as $key => $value) {
            $rukou_list[$key]['logo'] = json_img_add_url($value['logo']);
        }

        return return_msg(200, '', ['banner_list' => $banner_list, 'rukou_list' => $rukou_list]);
    }



    /**
    *
    *年龄段列表
    **/
    public function getAgeList()
    {
        $ageModel = new \app\home\model\AgeStageModel();
        $age_list = $ageModel->getAgeAll('id, name');//年龄段列表
        return return_msg(200, '', $age_list);
    }


    
    /**
    *
    *学科列表
    **/
    public function getSubjectList()
    {
        $subjectModel = new \app\home\model\SubjectModel();
        $couageModel = new \app\home\model\CourseAgeModel();
        $cousubjectModel = new \app\home\model\CourseSubjectModel();

        
        //接收年龄段参数,接受课程ID
        $age_id = $this->request->param('age_id', '', 'intval');


        //根据参数，查找对应学科
        if(!empty($age_id)){
            //课程ID集
            $course_count = $couageModel->getCourseAgeIdCount($age_id);
            
            //去查找对应的学科ID集，然后找学科
            $subject_id_count = $cousubjectModel->getCourseSubjectId($course_count);

            //学科列表
            $subject_list = $subjectModel->getWhereSubjectIdList($subject_id_count, 'id, name');
        }else{
            //学科列表
            $subject_list = $subjectModel->getSubjectAll('id, name');
        }
        return return_msg(200, '', $subject_list);
    }



    /**
    *
    *课程 - 查看更多
    **/
    public function getMoreCourseList()
    {

        $age_id = $this->request->param('age_id', '', 'intval');
        $subject_id = $this->request->param('subject_id', '', 'intval');
        $page = $this->request->param('page', 1, 'intval');

        $courseModel = new \app\home\model\CourseModel();
        $coulabelModel = new \app\home\model\CourseLabelModel();
        $couageModel = new \app\home\model\CourseAgeModel();
        $cousubjectModel = new \app\home\model\CourseSubjectModel();


        //开始查找
        if(!empty($age_id))
        {
            if(!empty($subject_id))
            {
                $course_count = $couageModel->getCourseAgeIdCount($age_id);
                $str = 'c.id, title, inventory_number, sell_money, original_money, basic_number, sales_number, tj_logo';
                $course_list = $cousubjectModel->getCourseWhereCourseCountsAndSujectId($course_count, $subject_id, $page, $str);
            }else{
                $str = 'c.id, title, inventory_number, sell_money, original_money, basic_number, sales_number, tj_logo';
                $course_list = $couageModel->getCourseWhereAgeId($age_id, $page, $str);
            }

        }else{
            //只是学科查找
            if(!empty($subject_id))
            {
                $str = 'c.id, title, inventory_number, sell_money, original_money, basic_number, sales_number, tj_logo';
                $course_list = $cousubjectModel->getCourseWhereSujectId($subject_id, $page, $str);
            }else{
                 //默认展示排序最前的三个课程
                $str = 'id, title, inventory_number, sell_money, original_money, basic_number, sales_number, tj_logo';
                $course_list = $courseModel->getIndexCourseList($str, $page);
            }
        }

        foreach ($course_list as $key => $value) {
            $course_list[$key]['label'] = $coulabelModel->getCourseLabelList($value['id'], 'id,label');
            $course_list[$key]['tj_logo'] = json_img_add_url('/upload/'.$value['tj_logo']);
            //$course_list[$key]['label']
        }
        return return_msg(200, '', $course_list);
    }
}
